home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0045_Turbovison Text Sort.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-05  |  1KB  |  36 lines

  1. {
  2.     Here is an example of a TStringCollection decendant that sorts the
  3.     strings by the 30th character and beyond.  The default
  4.     TStringCollection sorts from the first character. }
  5.  
  6.     uses Objects;
  7.  
  8.     type
  9.         PMyCollection   = ^TMyCollection;
  10.         TMyCollection   = object(TStringCollection)
  11.             function Compare(Key1, Key2 : Pointer); virtual;
  12.         end;
  13.  
  14.     function TMyCollection.Compare(Key1, Key2 : Pointer); virtual;
  15.     var s, t : string;
  16.     begin
  17.         { This is where you would sort two strings Compare must
  18.           return -1 if Key1 < Key2, 0 if Key1 = Key2, and
  19.           1 if Key1 > Key2 }
  20.         s := Copy(Key1^, 30, Length(Key1^) - 30);
  21.         t := Copy(Key2^, 30, Length(Key2^) - 30);
  22.         if s < t then Compare := -1 else
  23.          if s = t then Compare := 0 else
  24.            Compare := 1;
  25.     end;
  26.  
  27.     var P : PMyCollection;
  28.     begin
  29.        P := New(PMyCollection, Init(10, 10));
  30.        ReadLineFromFile;
  31.        Insert(NewStr(LineFromFile));
  32.        for x := 0 to P^.Count - 1 do
  33.         writeln(PString(P^.At(x))^);
  34.        Dispose(P, Done);
  35.     end;
  36.